package com.itdr.protal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itdr.common.Const;
import com.itdr.common.ServerResponse;
import com.itdr.pojo.Product;
import com.itdr.pojo.vo.ProductVO;
import com.itdr.protal.mapper.ProductMapper;
import com.itdr.protal.service.IProductService;
import com.itdr.utils.VOUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AirZhang
 * @since 2020-10-20
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Resource
    ProductMapper productMapper;

    @Override
    public ServerResponse pli(String keyWord,Integer pageNum,Integer pageSize) {
        // 1.参数处理
        String s = "%"+keyWord+"%";
        // 2.模糊查询数据，并分页
        Page<Product> lip = productMapper.selectPage(new Page<Product>(pageNum,pageSize), new QueryWrapper<Product>().like("name", keyWord));
        return ServerResponse.toSuccess(lip);
    }

    @Override
    public ServerResponse pxq(Integer pid) {
        // 1.商品是否存在
        Product product = productMapper.selectById(pid);
        if(product == null){
            return ServerResponse.toFail(Const.ProductEnum.NO_PRODUCT.getCode(),Const.ProductEnum.NO_PRODUCT.getDesc());
        }

        // 2.商品是否正常售卖
        if(!product.getStatus().equals(1)){
            return ServerResponse.toFail(Const.ProductEnum.OUT_PRODUCT.getCode(),Const.ProductEnum.OUT_PRODUCT.getDesc());
        }
        // 3.返回商品详情数据
        ProductVO productVO = VOUtil.getProductVO(product);
        return ServerResponse.toSuccess(productVO);
    }

    @Override
    public ServerResponse newProduct() {
        List<ProductVO> voList = new ArrayList<>();
        List<Product> productList = productMapper.selectList(new QueryWrapper<Product>().eq("is_new", 1));
        for (Product product : productList) {
            ProductVO productVO = VOUtil.getProductVO(product);
            voList.add(productVO);
        }
        return ServerResponse.toSuccess(voList);
    }
}
